package com.best.oasis.settlement.web.exception;

import org.springframework.flex.core.ExceptionTranslator;
import org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException;
import org.springframework.util.ClassUtils;

import flex.messaging.MessageException;

public class DataBaseExceptionTranslator implements ExceptionTranslator {

	public static final String MODIFIED_BY_OTHERS = "MODIFIED_BY_OTHERS";
	
	@Override
	public boolean handles(Class<?> clazz) {
		return ClassUtils.isAssignable(HibernateOptimisticLockingFailureException.class, clazz);
	}

	@Override
	public MessageException translate(Throwable t) {
		if(t instanceof HibernateOptimisticLockingFailureException){
			MessageException me = new MessageException();
			me.setCode(MODIFIED_BY_OTHERS);
			// @todo
			me.setMessage("数据被修改，请刷新！");
			me.setRootCause(t);
			return me;
		}
		return null;
	}
}
